package com.qianfeng.missfood.adapter;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.qianfeng.missfood.R;
import com.qianfeng.missfood.bean.PicListEntity;
import com.qianfeng.missfood.bean.ResultListEntity;
import com.qianfeng.missfood.fragment.GoodsPagerFragment;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Carmen on 2015/8/6.
 */
public class FoodAdapter extends AppBaseAdapter<ResultListEntity> {
    private static final int TYPE_COUNT = 4;//item类型的总数
    private static final int TYPE_CHANNEL = 2;//功能图标
    private static final int TYPE_ALBUM = 1;// 单张图片
    private static final int TYPE_GOODS = 0;//图片加介绍信息
    private static final int TYPE_KEYWORDS = 3;//食物分类
    private FragmentManager fm;
    private int currentType;//当前item类型

    /**
     * @param list
     * @param context
     */
    public FoodAdapter(List<ResultListEntity> list, Context context, FragmentManager fm) {
        super(list, context);
        this.fm = fm;
    }

    @Override
    public View getItemView(int position, View convertView, ViewGroup parent) {
        ChannelViewHolder channelViewHolder = null;
        AlbumViewHolder albumViewHolder = null;
        GoodsViewHolder goodsViewHolder = null;
        KeywordsViewHolder keywordsViewHolder = null;
        currentType = getItemViewType(position);


        if (convertView == null) {
            switch (currentType) {
                case TYPE_CHANNEL:
                    convertView = LayoutInflater.from(context).inflate(R.layout.item_food_channel, parent, false);
                    channelViewHolder = new ChannelViewHolder(convertView);
                    convertView.setTag(channelViewHolder);
                    break;
                case TYPE_ALBUM:
                    convertView = LayoutInflater.from(context).inflate(R.layout.item_food_album, parent, false);
                    albumViewHolder = new AlbumViewHolder(convertView);
                    convertView.setTag(albumViewHolder);
                    break;
                case TYPE_GOODS:
                    convertView = LayoutInflater.from(context).inflate(R.layout.item_food_goods, parent, false);
                    goodsViewHolder = new GoodsViewHolder(convertView);
                    convertView.setTag(convertView);
                    break;
                case TYPE_KEYWORDS:
                    convertView = LayoutInflater.from(context).inflate(R.layout.item_food_keywords, parent, false);
                    keywordsViewHolder = new KeywordsViewHolder(convertView);
                    convertView.setTag(keywordsViewHolder);

                    break;
            }
        } else {
            Object object = convertView.getTag();
            switch (currentType) {
                case TYPE_CHANNEL:
                    if (object instanceof ChannelViewHolder) {
                        channelViewHolder = (ChannelViewHolder) convertView.getTag();
                    } else {
                        convertView = LayoutInflater.from(context).inflate(R.layout.item_food_channel, parent, false);
                        channelViewHolder = new ChannelViewHolder(convertView);
                        convertView.setTag(channelViewHolder);
                    }
                    break;
                case TYPE_ALBUM:
                    if (object instanceof AlbumViewHolder) {
                        albumViewHolder = (AlbumViewHolder) convertView.getTag();
                    } else {
                        convertView = LayoutInflater.from(context).inflate(R.layout.item_food_album, parent, false);
                        albumViewHolder = new AlbumViewHolder(convertView);
                        convertView.setTag(albumViewHolder);
                    }
                    break;
                case TYPE_GOODS:
                    if (object instanceof GoodsViewHolder) {
                        goodsViewHolder = (GoodsViewHolder) convertView.getTag();
                    } else {
                        convertView = LayoutInflater.from(context).inflate(R.layout.item_food_goods, parent, false);
                        goodsViewHolder = new GoodsViewHolder(convertView);
                        convertView.setTag(convertView);
                    }
                    break;
                case TYPE_KEYWORDS:
                    if (object instanceof KeywordsViewHolder) {
                        keywordsViewHolder = (KeywordsViewHolder) convertView.getTag();
                    } else {
                        convertView = LayoutInflater.from(context).inflate(R.layout.item_food_keywords, parent, false);
                        keywordsViewHolder = new KeywordsViewHolder(convertView);
                        convertView.setTag(keywordsViewHolder);
                    }
                    break;
            }
        }

        switch (currentType) {
            case TYPE_CHANNEL:
                channelViewHolder.gridView.setAdapter(new FoodChannelAdapter(list.get(position).getChannelItem(), context));
                break;
            case TYPE_ALBUM:
                bitmapUtils.display(albumViewHolder.imageView, list.get(position).getAlbumItem().getCoverUrl());
                break;
            case TYPE_GOODS:
                List<PicListEntity> picList = list.get(position).getGoodsItem().getPicList();
                if (picList != null && !picList.isEmpty()) {
                    ArrayList<Fragment> fgs = new ArrayList<>();
                    for (PicListEntity pic : picList) {
                        fgs.add(GoodsPagerFragment.newInstance(pic.getThumbUrl()));
                    }
                    goodsViewHolder.viewPager.setAdapter(new GoodsPagerAdapter(fgs, fm));
                }
                bitmapUtils.display(goodsViewHolder.head, list.get(position).getGoodsItem().getShop().getChefIcon());
                goodsViewHolder.title.setText(list.get(position).getGoodsItem().getName());
                goodsViewHolder.name.setText(list.get(position).getGoodsItem().getShop().getShopName());
                goodsViewHolder.content.setText(list.get(position).getGoodsItem().getDesc());
                goodsViewHolder.price.setText("￥" + (list.get(position).getGoodsItem().getPrice()) / 100);
                goodsViewHolder.love.setText("" + list.get(position).getGoodsItem().getFavoriteNum());
                break;
            case TYPE_KEYWORDS:
                keywordsViewHolder.gridView.setAdapter(new FoodKeywordsAdapter(list.get(position).getKeywordsItem(), context));
                break;
        }

        return convertView;
    }

    @Override
    public int getViewTypeCount() {
        return TYPE_COUNT;
    }

    @Override
    public int getItemViewType(int position) {
        // TODO Auto-generated method stub
        if (3 == list.get(position).getItemType()) {
            return TYPE_ALBUM;//
        } else if (6 == list.get(position).getItemType()) {
            return TYPE_CHANNEL;//
        } else if (1 == list.get(position).getItemType()) {
            return TYPE_GOODS;//
        } else if (5 == list.get(position).getItemType()) {
            return TYPE_KEYWORDS;//
        }
        return 0;
    }

    public static class ChannelViewHolder {
        @ViewInject(R.id.food_channel)
        private GridView gridView;

        public ChannelViewHolder(View convertView) {
            ViewUtils.inject(this, convertView);
        }
    }

    public static class AlbumViewHolder {
        @ViewInject(R.id.food_album_iv)
        private ImageView imageView;

        public AlbumViewHolder(View convertView) {
            ViewUtils.inject(this, convertView);
        }

    }

    public static class GoodsViewHolder {
        @ViewInject(R.id.food_goods_vp)
        private ViewPager viewPager;
        @ViewInject(R.id.food_goods_head)
        private ImageView head;
        @ViewInject(R.id.food_goods_title)
        private TextView title;
        @ViewInject(R.id.food_goods_name)
        private TextView name;
        @ViewInject(R.id.food_goods_content)
        private TextView content;
        @ViewInject(R.id.food_goods_price)
        private TextView price;
        @ViewInject(R.id.food_goods_love)
        private TextView love;

        public GoodsViewHolder(View convertView) {
            ViewUtils.inject(this, convertView);
        }
    }

    public static class KeywordsViewHolder {
        @ViewInject(R.id.food_keywords_gv)
        private GridView gridView;

        public KeywordsViewHolder(View convertView) {
            ViewUtils.inject(this, convertView);
        }
    }
}
